$(function(){
        $(".inner-left").hover(function(){
            $(this).children("a").css("color","#fa4b9b");
        },function(){
            $(this).children("a").css("color","#666");
        });
        $(".inner-right").find("li").hover(function(){
            $(this).children("a").css("color","#fa4b9b");
        },function(){
            $(this).children("a").css("color","#666");
        });





    $(".footer").load("./footer.html", function(){
        $(".help_l dl dd a").hover(function(){
            $(this).css("color","#fa4b9b");
        },function(){
            $(this).css("color","");
        });

        $(".help_r a").eq(1).hover(function(){
            $(".wx_code").css("display","block")
        },function(){
            $(".wx_code").css("display","")
        });

        $(".footer_link").eq(0).find("a").hover(function(){
            $(this).css("color","#fa4b9b");
        },function(){
            $(this).css("color","");
        });
    })
    $(window).scroll(function(){
        let $scroll=$(this).scrollTop();
        if($scroll>$("header").height()){
            $(".rightbar").css("display","block");
            $(".advertise").css("display","block");
            $(".leftbar").css("display","block");
        }else{
            $(".rightbar").css("display","");
            $(".advertise").css("display","");
            $(".leftbar").css("display","");
        }
    })
    $(".rightbar li a").eq(1).on("click",function(){
        alert("请登录后再来点我哦")
        location="login.html";
        return false;
    })

    $(".rightbar li").hover(function(){
        $(this).css("background","#fafafa");
    },function(){
        $(this).css("background","");
    });
    $(".top").on("click",function(){
        $('html , body').animate({scrollTop: 0},400);
    })

    var userInfo=null;
    var jsonStr=sessionStorage.getItem("login");
    userInfo=JSON.parse(jsonStr||'{}');

    if(userInfo.uname!=undefined){
        $("#login").text(userInfo.uname);
    }


    //动态显示购物车

    if($("#login").text()=="登录"){
        const dataCookie=JSON.parse($.cookie("cartInfo")||'[]');

        if(dataCookie.length==0){
            const strHTMLA=`
            
    <div class="empty">
        <p class="f30">您还没有为您可爱的宝宝挑选任何商品哦~</p>
        <p>
            您可以去
            <a href="index.html">首页</a>
            挑选宝宝喜欢的商品
        </p>
    </div>

            `;
            $(".shopping_cart").html(strHTMLA);
        }else{
            dataCookie.forEach((el,index)=>{
                $(".shopping_cart").attr("gId",`${el.gId}`);
                const strHTMLB=`
              
    <div class="cart_top">
        <span>我的购物车</span>
    </div>
    <div class="cart_menu">
        <div class="choose">
            <div class="check" id="all">
                <input type="checkbox"/>
            </div>
            <label>全选</label>
        </div>
        <div class="product">商品</div>
        <div class="price">单价</div>
        <div class="quantity">数量</div>
        <div class="summery">小计</div>
        <div class="operate">操作</div>
    </div>
    <div class="cart_list">
        <div class="list_detail">
            <div class="detail_input check">
                <input type="checkbox"/>
            </div>
            <div class="detail_img">
                <a href="goods.html"><img src="images/detail_02.jpg" alt=""></a>
            </div>
            <div class="description">
                <p>
                    <a href="#">${el.gName}</a></p>
                <p>${el.gSize}</p>
            </div>
            <div class="detail_price">
                <p class="new">${el.gNewPrice}</p>
                <p class="old"><s>${el.gOldPrice}</s></p>
            </div>
            <div class="detail_num">
                <span class="jian"></span>
                <em class="number">${el.num}</em>
                <span class="jia"></span>
            </div>
            <div class="total">
                <span>${el.total}</span>
            </div>
            <div class="detail_operate">
                <p class="delete">
                    <a href="#" id="delete">删除</a></p>
            </div>
        </div>
    </div>
    
                
    </div>
    <div class="cashier">
        <div class="cashier_left">
            <a href="#" id="removeFail">清除无效商品</a>
            <a href="#" id="addToMine">移入收藏夹</a>
        </div>
        <div class="newCart">
                <span>
                    已选择商品
                    <b class="select_num">0</b>
                     件
                </span>
            <span>
                    合计（不含运费和进口税）：
                    <i class="rmb" style="font-size:18px;color: #fa4b9b;font-weight: 400;vertical-align:top;">¥</i>
                    <b class="amount">0</b>
                </span>

        </div>
        <a href="#" class="btnjiesuan">去结算</a>
    </div>
                `
                $(".shopping_cart").html(strHTMLB);
            })
        }
    }else{
        $.ajax({
            url:"./../server/showCart.php",
            dataType:"json",
            data:userInfo,
            type:"post"
        }).then(function(res){
            if(res==0){
                const strHTMLA=`

    <div class="empty">
        <p class="f30">您还没有为您可爱的宝宝挑选任何商品哦~</p>
        <p>
            您可以去
            <a href="index.html">首页</a>
            挑选宝宝喜欢的商品
        </p>
    </div>

            `;
                $(".shopping_cart").html(strHTMLA);
            }else{
                res.forEach((el,index)=>{
                    $(".shopping_cart").attr("gId",`${el.gId}`);
                    const strHTMLB=`

    <div class="cart_top">
        <span>我的购物车</span>
    </div>
    <div class="cart_menu">
        <div class="choose">
            <div class="check" id="all">
                <input type="checkbox"/>
            </div>
            <label>全选</label>
        </div>
        <div class="product">商品</div>
        <div class="price">单价</div>
        <div class="quantity">数量</div>
        <div class="summery">小计</div>
        <div class="operate">操作</div>
    </div>
    <div class="cart_list">
        <div class="list_detail">
            <div class="detail_input check">
                <input type="checkbox"/>
            </div>
            <div class="detail_img">
                <a href="goods.html"><img src="images/detail_02.jpg" alt=""></a>

            </div>
            <div class="description">
                <p>
                    <a href="#">${el.gName}</a></p>
                <p>${el.gSize}</p>
            </div>
            <div class="detail_price">
                <p class="new">${el.gNewPrice}</p>
                <p class="old"><s>${el.gOldPrice}</s></p>
            </div>
            <div class="detail_num">
                <span class="jian"></span>
                <em class="number">${el.num}</em>
                <span class="jia"></span>
            </div>
            <div class="total">
                <span>${el.gTotal}</span>
            </div>
            <div class="detail_operate">
                <p class="delete">
                    <a href="#" id="delete">删除</a></p>
            </div>
        </div>
    </div>
    <div class="cashier">
        <div class="cashier_left">
            <a href="#" id="removeFail">清除无效商品</a>
            <a href="#" id="addToMine">移入收藏夹</a>
        </div>
        <div class="newCart">
                <span>
                    已选择商品
                    <b class="select_num">0</b>
                     件
                </span>
            <span>
                    合计（不含运费和进口税）：
                    <i class="rmb" style="font-size:18px;color: #fa4b9b;font-weight: 400;vertical-align:top;">¥</i>
                    <b class="amount">0</b>
                </span>

        </div>
        <a href="#" class="btnjiesuan">去结算</a>
    </div>
                `

                    $(".shopping_cart").html(strHTMLB);
                })
            }
        })
    }


    //购物车处理
    //    选中未选中
        var num=0;
        var amount=0;
        $(".shopping_cart").on("click",".detail_input",function(){
            var detailArr=[];
            if($(this).hasClass("checked")){
                $(this).removeClass("checked");
                num--;
                if(num<=0){
                    $(".btnjiesuan").css({
                        backgroundColor: "",
                        cursor: "",
                        border: "",
                    })
                }
                amount=+parseInt(amount)-parseInt($(this).parent(".list_detail").find(".total span").text());
                $(".amount").text(amount);
            }else{
                num++
                $(this).addClass("checked");
                if(num>0){
                    $(".btnjiesuan").css({
                        backgroundColor: "#fa4b9b",
                        cursor: "pointer",
                        border: "1px solid #fa4b9b",
                    })
                }
                amount=parseInt($(this).parent(".list_detail").find(".total span").text())+parseInt(amount);
                $(".amount").text(amount);
            }
            $(".select_num").text(num);


            $(".detail_input").each((index,el)=>{
                detailArr.push(el);
            })
            if(num==detailArr.length){
                $("#all").addClass("checked");
            }else{
                $("#all").removeClass("checked");
            }
        })

    //全选
    $(".shopping_cart").on("click","#all",function(){
        var amount=0;
        if($(this).hasClass("checked")){
            $(this).removeClass("checked");
            $(".detail_input").removeClass("checked");
            $(".btnjiesuan").css({
                backgroundColor: "",
                cursor: "",
                border: "",
            })
            $(".amount").text(0);
            $(".select_num").text(0);
        }else{
            $(this).addClass("checked");
            $(".detail_input").addClass("checked");
            $(".btnjiesuan").css({
                backgroundColor: "#fa4b9b",
                cursor: "pointer",
                border: "1px solid #fa4b9b",
            })
            $(".detail_input").each((index,el)=>{
                amount=parseInt($(el).parent(".list_detail").find(".total span").text())+amount;
            })
            $(".amount").text(amount);
            $(".select_num").text($(".detail_input").length);
        }

    })
    //加号事件
    $(".shopping_cart").on("click",".jia",function(){
        var proNum=$(this).prev("em").text();
        proNum=parseInt(proNum)+1;
        if(proNum>99){
            proNum=99;
            $(this).prev("em").text(proNum);
            confirm("商品数量不能大于99");
        }else{
            $(this).prev("em").text(proNum);
            $(this).parents(".list_detail").find(".total span").text(parseInt(proNum)*parseInt($(this).parents(".list_detail").find(".detail_price .new").text()));

            if($("#login").text()=="登录"){
                const dataCookie=JSON.parse($.cookie("cartInfo")||'[]');
                dataCookie.forEach((el,index)=>{
                    if(el.gId==$(".shopping_cart").attr("gId")){
                        el.num=$(this).prev("em").text();
                        el.total=parseInt(el.num)*parseInt(el.gNewPrice);
                    }
                    $.cookie("cartInfo",JSON.stringify(dataCookie),{expires:7});
                })

            }else{
                console.log("登录状态");
                $.ajax({
                    url:"./../server/updateCart.php",
                    type:"post",
                    data:{uid:userInfo.uid,gId:$(".shopping_cart").attr("gId"),num:$(this).prev("em").text(),gTotal: $(this).parents(".list_detail").find(".total span").text()},
                    dataType:"json"
                }).then(function(res){
                    confirm(res.msg);
                })
            }
        }
    })

    //减号事件
    $(".shopping_cart").on("click",".jian",function(){
        var proNum=$(this).next("em").text();
        proNum=parseInt(proNum)-1;
        if(proNum<1){
            proNum=1;
            $(this).next("em").text(proNum);
            confirm("商品数量不能小于1");
        }else{
            $(this).next("em").text(proNum);
            $(this).parents(".list_detail").find(".total span").text(parseInt(proNum)*parseInt($(this).parents(".list_detail").find(".detail_price .new").text()));
            if($("#login").text()=="登录"){
                const dataCookie=JSON.parse($.cookie("cartInfo")||'[]');
                dataCookie.forEach((el,index)=>{
                    if(el.gId==$(".shopping_cart").attr("gId")){
                        console.log($(this));
                        el.num=$(this).next("em").text();
                        el.total=parseInt(el.num)*parseInt(el.gNewPrice);
                    }
                    $.cookie("cartInfo",JSON.stringify(dataCookie),{expires:7});
                })
            }else{
                $.ajax({
                    url:"./../server/updateCart.php",
                    type:"post",
                    data:{uid:userInfo.uid,gId:$(".shopping_cart").attr("gId"),num:$(this).next("em").text(),gTotal: $(this).parents(".list_detail").find(".total span").text()},
                    dataType:"json"
                }).then(function(res){
                    confirm(res.msg);
                })
            }
        }
    })

    //删除事件
    $(".shopping_cart").on("click","#delete",function(){
        if($("#login").text()=="登录"){
        //    未登录时
            const dataCookie=JSON.parse($.cookie("cartInfo")||'[]');
            dataCookie.forEach((el,index)=>{
                console.log(el.gId);
                if(el.gId==$(".shopping_cart").attr("gId")){
                    dataCookie.splice(index,1);
                    confirm("删除成功！");
                    window.history.go(0);
                }
            })
            $.cookie("cartInfo",JSON.stringify(dataCookie),{expires:7});
        }else{
        //    登录时
            $.ajax({
                url:"./../server/deleteCart.php",
                type:"post",
                dataType:"json",
                data:{uId:userInfo.uid,gId:$(".shopping_cart").attr("gId")}
            }).then(function(res){
                alert(res.msg);
                window.history.go(0);
            })
        }








        return false;
    })

    //结算事件
    $(".shopping_cart").on("click",".btnjiesuan",function(){
        if($("#login").text()=="登录"){
            //未登录状态
            // console.log($(this).parents(".shopping_cart").find(".choose .check"));
            alert("请先登录哦");
            window.location.href="login.html";
        }else{
        //    登录状态
            if($(".amount").text()!=0){
                alert("前往付款界面") ;

            }else{
                alert("未选中商品");

            }
        }
        return false;
    })


    //选中删除事件

})